home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / c_course.zip / LISTF.C < prev    next >
Text File  |  1989-12-30  |  7KB  |  153 lines

  1. /* *************************************************************** */
  2. /* This module contains the functions called by the list.c program */
  3. /* program. If this were a program to be used for some specific    */
  4. /* purpose, it would probablly not be wise to break it up into two */
  5. /* separately compiled modules. It is only done here for purposes  */
  6. /* of illustration. It is a useful program.                        */
  7. /* *************************************************************** */
  8.  
  9. #define MAXLINES 54            /* maximum number of lines per page */
  10. #include "stdio.h"                     /* standard I/O header file */
  11. extern FILE *file_point;         /* pointer to the file to be read */
  12. extern FILE *print_file_point;           /* pointer to the printer */
  13. extern char oneline[];                 /* input string buffer area */
  14. char filename[15];               /* filename from header or prompt */
  15. int line_number = 0;             /* line number initialized to one */
  16. int page_number = 1;             /* page number initialized to one */
  17. int lines_this_page = 0;              /* lines on this page so far */
  18.  
  19.  
  20. /* ***************************************************** open_file */
  21. /* This function opens the input file named on the command line,   */
  22. /* if there was one defined. Otherwise, it requests a file name to */
  23. /* open and opens the requested file.                              */
  24. /* *************************************************************** */
  25. open_file(no,name)
  26. int no;                     /* number of arguments on command line */
  27. char *name;                    /* first argument from command line */
  28. {
  29.  
  30.    strcpy(filename,name);         /* copy name for printing header */
  31.    file_point = NULL;           /* if no name was given in command */
  32.    if (no == 2) {              /* 2nd field in command is filename */
  33.       file_point = fopen(name,"r");         /* open requested file */
  34.       if (file_point == NULL)        /* NULL if file doesn't exist */
  35.          printf("Filename on command line doesn't exist!\n");
  36.    }
  37.  
  38.    do {
  39.       if (file_point == NULL) {                 /* no filename yet */
  40.          printf("Enter filename -> ");
  41.          scanf("%s",filename);
  42.          file_point = fopen(filename,"r");            /* open file */
  43.          if (file_point == NULL)          /* NULL if file no exist */
  44.             printf("Filename doesn't exist, try again.\n");
  45.       }
  46.    } while (file_point == NULL);   /* continue until good filename */
  47. }
  48.  
  49.  
  50. /* *********************************************** open_print_file */
  51. /* This function opens the printer file to the standard printer.   */
  52. /* *************************************************************** */  
  53. open_print_file()
  54. {
  55.    print_file_point = fopen("PRN","w");       /* open printer file */
  56. }
  57.  
  58.  
  59. /* ************************************************** print_a_line */
  60. /* This routine prints a line of text and checks to see if there   */
  61. /* is room for another line on the page. If not, it starts a new   */
  62. /* page with a new header. This routine calls several other local  */
  63. /* routines.                                                       */
  64. /* *************************************************************** */
  65. print_a_line()
  66. {
  67. int index;
  68.  
  69.    header();
  70.    printf("%5d %s",line_number,oneline);
  71.  
  72.                        /* This prints a line of less than 72 chars */
  73.    if (strlen(oneline) < 72)
  74.       fprintf(print_file_point,"%5d %s",line_number,oneline);
  75.  
  76.                           /* This prints a line of 72 to 143 chars */
  77.    else if (strlen(oneline) < 144) {
  78.       fprintf(print_file_point,"%5d ",line_number);
  79.       for (index = 0;index < 72;index++)
  80.          fprintf(print_file_point,"%c",oneline[index]);
  81.       fprintf(print_file_point,"<\n      ");
  82.       for (index = 72;index < strlen(oneline);index++)
  83.          fprintf(print_file_point,"%c",oneline[index]);
  84.       lines_this_page++;
  85.    }
  86.  
  87.                           /* This prints a line of 144 to 235 chars */
  88.    else if (strlen(oneline) < 235) {
  89.       fprintf(print_file_point,"%5d ",line_number);
  90.       for (index = 0;index < 72;index++)
  91.          fprintf(print_file_point,"%c",oneline[index]);
  92.       fprintf(print_file_point,"<\n      ");
  93.       for (index = 72;index < 144;index++)
  94.          fprintf(print_file_point,"%c",oneline[index]);
  95.       fprintf(print_file_point,"<\n      ");
  96.       for (index = 144;index < strlen(oneline);index++)
  97.          fprintf(print_file_point,"%c",oneline[index]);
  98.       lines_this_page += 2;
  99.    }
  100.          /* the following line outputs a newline if there is none
  101.                                       at the end of the last line */
  102.    if (oneline[strlen(oneline)-1] != '\n')
  103.       fprintf(print_file_point,"%c",'\n');
  104.    line_number++;
  105.    lines_this_page++;
  106. }
  107.  
  108.  
  109. /* ******************************************************** header */
  110. /* This routine checks to see if a header needs to be printed. It  */
  111. /* also checks for the end of a page. and spaces the paper up.     */
  112. /* *************************************************************** */
  113. header()
  114. {
  115. int index;
  116.  
  117.                   /* first see if we are at the bottom of the page */
  118.    if (lines_this_page > MAXLINES) {  /* space paper up for bottom */
  119.    for (index = lines_this_page;index < 61;index++)
  120.       fprintf(print_file_point,"\n");
  121.    lines_this_page = 0;
  122.    }
  123.  
  124.             /* put a monitor header out only at the very beginning */
  125.    if (line_number == 0) {               /* display monitor header */
  126.       printf("        Source file %s\n",filename);
  127.       line_number = 1;
  128.    }
  129.  
  130.          /* check to see if we are at the top of the page either   */ 
  131.          /* through starting a file, or following a bottom of page */
  132.    if (lines_this_page == 0) {        /* top of every printer page */
  133.       fprintf(print_file_point,"\n\n\n        ");
  134.       fprintf(print_file_point," Source file - %s        ",filename);
  135.       fprintf(print_file_point,"          Page %d\n\n", page_number);
  136.       page_number++;
  137.    }
  138. }
  139.  
  140.  
  141. /* *************************************************** top_of_page */
  142. /* This function spaces the paper to the top of the next page so   */
  143. /* that another call to this function will start correctly. This   */
  144. /* is used only at the end of a complete printout.                 */
  145. /* *************************************************************** */
  146. top_of_page()
  147. {
  148. int index;
  149.  
  150.    for (index = lines_this_page;index < 61;index++)
  151.       fprintf(print_file_point,"\n");
  152. }
  153.